#!/bin/bash

if [ "$1" == "ul" ]
then
        spellsite="http://www.undeadspells.com/overlords/showspells.php"
        website="www.undeadlive.com"
        own_id=223980302
        game=undead
        serverid=_undead_j2
        old_code_file=oldspells
        ok="Success:"
        nok="Failure:"
elif [ "$1" == "eow" ]
then
        spellsite="http://www.undeadspells.com/eow/showspells.php"
        website="www.enginesofwar.com"
        own_id=134463201
        game=army
        serverid=army_1
        old_code_file=oldkeys
        ok="Hoo Rah:"
        nok="Negative:"
else
        echo "unknown parameter"
        exit
fi

echo "website is : $website"
echo "game is : $game"
echo "serverid : $serverid"

[ ! -f "$old_code_file" ] && touch "$old_code_file"

if [ -f /home/sioban/.mozilla/firefox-3.5/2gufwxr0.default/cookies.sqlite ]
then
        /bin/cp /home/sioban/.mozilla/firefox-3.5/2gufwxr0.default/cookies.sqlite .
        session=$(sqlite3 -line cookies.sqlite 'select value from moz_cookies where name = "_'${game}'_session" order by expiry DESC;' | head -1 | sed "s/value = //")
else
        session="$(cat cookie_$game)"
fi
echo "found session : $session"

> slayer.list

zeroes=10

while [ $zeroes -gt 0 ]
do
	return="$(curl -s -H "Host: $website" \
            		-H "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2" \
                	-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
                	-H "Accept-Language: en-us,en;q=0.5" \
                	-H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" \
                	-H "Keep-Alive: 300" \
                	-H "Connection: keep-alive" \
                	-H "Cookie: _${game}_session=$session; SERVERID=$serverid" \
               	http://$website/contract/list)"

        if [[ "$return" =~ "relogin" ]]
	then
        	echo "you must reset the session cookie"
        	exit -1
        fi

	echo $return | sed "s/Top Bounties.*//" | sed "s/</\n</g" > slay.list

	OLD_IFS=$IFS

	IFS="
"

	u=0
	for line in $(cat slay.list)
	do
		profile="$(echo $line | sed "s@<a href=\"\([^\"]*\)\" class=\"username\">\(.*\)@\1:\2@p;d")"

		if [ "$profile" ]
		then
			url="http://${website}$(echo $profile|cut -d":" -f1)"
			contract_id="$(echo $url|cut -d"=" -f3)"
			name="$(echo $profile|cut -d":" -f2)"

			if [ "$name" ]
			then
				gold_raw="$(grep "ASJS.Contract.attempt('$contract_id')" slay.list | sed "s/.*>.\([0-9.]*\)\(.*\)/\1:\2/")"
				gold="$(echo $gold_raw|cut -d":" -f1)"
				unit="$(echo $gold_raw|cut -d":" -f2)"
				agold="${gold}${unit}"

				[ "$unit" = "M" ] && gold="$(echo "$gold * 1000" | bc)"
				[ "$unit" = "B" ] && gold="$(echo "$gold * 1000000" | bc)"

				if [ ! "$(grep $contract_id slayer.list)" ]
				then
					echo "$gold ($agold) $url $name" >> slayer.list
					let u=u+1
				fi
			fi
		fi
	done

	if [ $u -gt 0 ]
	then
		echo "added $u users"
		zeroes=5
	else
		echo "None added ($zeroes)"
		let zeroes=zeroes-1
	fi
done

cat slayer.list | sort -n
